package com.centling.domain;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Session {

	
	private static Map<String,String> map = new HashMap<String,String>();
	
	static{
		map  = new HashMap<String,String>();
		map.put("stu_id", "stuId");
		map.put("stu_name", "stuName");
		map.put("stu_email", "stuEmail");
	}
	
	
	
	
	
	public void save(Student s ) throws SQLException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		
		Connection connect = getConnection();
		String colNames = findCols(map);
		String sql = "insert into student("+colNames+") values(?,?,?)";
		PreparedStatement statement = connect.prepareStatement(sql);
		
		Set<String> keySet = map.keySet();
		int i =0;
		for(String e :  keySet){
			
			//通过key，找到property值
			String propertyName = map.get(e);
			
			//获取get方法
			String methodName = "get"+Character.toUpperCase(propertyName.charAt(0))+propertyName.substring(1);
			Method method = s.getClass().getMethod(methodName);
			
			
			//获取返回值及返回值的类型
			Class returnType = method.getReturnType();
			String returnTypeName = returnType.getName();
			
			System.out.println(method.getName()+"---"+returnTypeName);
			
			if("java.lang.String".equals(returnTypeName)){
				String tempString = (String) method.invoke(s);//相当于调取student.getPropertyName方法
				statement.setString(i+1, tempString);
				
			}else if("int".equals(returnTypeName)){
				Integer tempString = (Integer) method.invoke(s);//相当于调取student.getPropertyName方法
				statement.setInt(i+1, tempString);
				
			}
			
			i++;
		}
		statement.executeUpdate();
		
	}
	
	
	/**
	 * 查询cols的字符串
	 * @param map
	 * @return
	 */
	public String findCols(Map<String,String> map){
		
		Set<String> set = map.keySet();
		StringBuffer buffer = new StringBuffer();
		for(String e : set){
			String propertyName = map.get(e);
			propertyName = Character.toUpperCase(propertyName.charAt(0))+propertyName.substring(1);
			String upperElement = e.toUpperCase();
			buffer.append(upperElement+",");
		}
		
		int index = buffer.lastIndexOf(",");
		if(index != -1){
			buffer.deleteCharAt(index);
		}
		return buffer.toString();
	}
	
	
	
	/**
	 * 查找get方法的名字
	 * @param map
	 * @return
	 */
	public List<String> findGetMethodNames(Map<String,String> map){
		
		Set<String> set = map.keySet();
		
		List<String> list = new ArrayList<String>();
		for(String e : set){
			String propertyName = map.get(e);
			propertyName = Character.toUpperCase(propertyName.charAt(0))+propertyName.substring(1);
			list.add(propertyName);
		}
		
		return list;
	}
	
	/**
	 * 获取connection
	 * @return
	 */
	public  static Connection  getConnection(){
		
		Connection connect = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate", "root", "123456");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("没有找到com.mysql.driver.Driver类");
		}catch (SQLException e) {
			e.printStackTrace();
			System.out.println("连接数据库失败");
		}
		
		return connect;
	}
}
